package com.querycenter.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.querycenter.model.DataAFiledAClass;
import com.querycenter.model.UserInfo;
import com.querycenter.serviceinter.AuthManageServiceInter;

import net.sf.json.JSONArray;

public class AuthManageAction {

	private AuthManageServiceInter AuthManageService;

	public void getData() {

		List<UserInfo> user = AuthManageService.selectAllUser();
		List<DataAFiledAClass> condition = AuthManageService
				.selectAllCondition();
		List<DataAFiledAClass> result = AuthManageService.selectAllResult();

		/*
		 * System.out.println(u.get(0).toString());
		 * System.out.println(d.get(0).toString());
		 * System.out.println(dd.get(0).toString());
		 */

		JSONArray jsonList;
		JSONArray jsonAllList = new JSONArray();
		jsonList = JSONArray.fromObject(user);
		jsonAllList.add(jsonList);
		jsonList = JSONArray.fromObject(condition);
		jsonAllList.add(jsonList);
		jsonList = JSONArray.fromObject(result);
		jsonAllList.add(jsonList);
		/*
		 * UserInfo uu = new UserInfo(); uu.setId(1); List<UserInfo> list1 = new
		 * ArrayList<UserInfo>(); list1.add(uu); UserInfo uu1 = new UserInfo();
		 * uu1.setId(2); list1.add(uu1); List<UserInfo> list2 = new
		 * ArrayList<UserInfo>(); list2.add(uu); List<UserInfo> list3 = new
		 * ArrayList<UserInfo>(); list3.add(uu); JSONArray jsonAllList = new
		 * JSONArray(); JSONArray jsonList = JSONArray.fromObject(list1);
		 * jsonAllList.add(jsonList); jsonList = JSONArray.fromObject(list2);
		 * jsonAllList.add(jsonList); jsonList = JSONArray.fromObject(list3);
		 * jsonAllList.add(jsonList);
		 */

		System.out.println(jsonAllList);
		ActionContext context = ActionContext.getContext();
		ServletActionContext.getResponse()
				.setContentType("text/html;charset=UTF-8");
		HttpServletResponse response = (HttpServletResponse) context
				.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.println(jsonAllList);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 给用户添加权限
	public void addRelation() {
		System.out.println("a");

		HttpServletRequest request = ServletActionContext.getRequest();
		String user = request.getParameter("user");
		String Paraobject = request.getParameter("object");
		String Paraclassify = request.getParameter("classify");
		String Parafield = request.getParameter("field");
		String[] object = Paraobject.split(",");
		String[] classify = Paraclassify.split(",");
		String[] field = Parafield.split(",");

		for (int i = 0; i < object.length; i++) {

			AuthManageService.addObject_user(Integer.parseInt(user),
					Integer.parseInt(object[i]));

		}
		for (int i = 0; i < classify.length; i++) {

			AuthManageService.addClassify_user(Integer.parseInt(user),
					Integer.parseInt(classify[i]));

		}
		for (int i = 0; i < field.length; i++) {

			AuthManageService.addField_user(Integer.parseInt(user),
					Integer.parseInt(field[i]));

		}

	}

	public AuthManageServiceInter getAuthManageService() {
		return AuthManageService;
	}

	public void setAuthManageService(AuthManageServiceInter AuthManageService) {
		this.AuthManageService = AuthManageService;
	}
}
